//
//  RAPWelcomeViewController.swift
//  COPYCAT-Weibo
//
//  Created by 闫萌 on 12/30/15.
//  Copyright © 2015 RendezvousAuParadis. All rights reserved.
//

import UIKit
import SDWebImage

class RAPWelcomeViewController: UIViewController {
    
    // MARK: - 懒加载控件
    private lazy var iconView: UIImageView = {
        
        let imgView = UIImageView()
        
        if let avatar = RAPAccountViewModel.sharedViewModel.readAccountInfo()?.avatar_large {
            imgView.sd_setImageWithURL(NSURL(string: avatar)!, placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        imgView.layer.cornerRadius = 40
        imgView.layer.masksToBounds = true
        return imgView
        }()
    
    private lazy var messageLabel: UILabel = {
        let label = UILabel(text: "欢迎回来", fontSize: 16)
        label.alpha = 0
        return label
        }()
    
    private var iconViewVerticalConstraint: NSLayoutConstraint?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor(white: 237 / 255, alpha: 1.0)
        setupUI()
    }
    
    func setupUI() {
        // 设置头像
        view.addSubview(iconView)
        // 设置约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        // x
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: view, attribute: .CenterX, multiplier: 1, constant: 0))
        iconViewVerticalConstraint = NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1, constant: -50)
        view.addConstraint(iconViewVerticalConstraint!)
        // size
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        
        // 设置欢迎文字
        view.addSubview(messageLabel)
        view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: iconView, attribute: .Bottom, multiplier: 1, constant: 10))
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        iconViewVerticalConstraint?.constant = -150
        
        // 设置动画效果
        UIView.animateWithDuration(2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
            UIView.animateWithDuration(1, animations: { () -> Void in
                self.messageLabel.alpha = 1
                }, completion: { (_) -> Void in
                    // 发送通知，进入主界面
                    NSNotificationCenter.defaultCenter().postNotificationName(RAPEnterMainInterfaceNotification, object: self)
            })
        }
    }

}
